﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using Century.EnterpriseLibrary.Validation.DbProvider;
using System.Text.RegularExpressions;
using System.Net.Mail;
using Century.EnterpriseLibrary.Globalization;
using Century.Web.UI.MVC;

namespace Century.EnterpriseLibrary.Validation
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class DynamicValidatorAttribute : ValidationAttribute
    {

        public ValidationRule validationRule { get; set; }



        public DynamicValidatorAttribute()
        {

        }

        public override bool IsValid(object value)
        {

            if (validationRule != null)
            {

                string errorMessageKey = string.Format("GLB.Validation.{0}.{1}.{2}.", validationRule.Domain, validationRule.PageName, validationRule.FieldName);

                //IsRequired will not check on server side

                //if (validationRule.IsRequired)
                //{
                //    errorMessageKey += "IsRequired";
                //    this.ErrorMessage = GlobalizationManager.Translate(errorMessageKey, validationRule.FieldName + " Is Required");
                //    if (value == null || value.ToString() == string.Empty)
                //    {
                //        return false;
                //    }
                //}

                //if field is null or string empty must return true and should not check for validation as it does not have any value.
                if (value == null || value.ToString() == string.Empty)
                {
                    return true;
                }
                if (validationRule.MinLength.HasValue)
                {
                    errorMessageKey += "MinLength";
                    this.ErrorMessage = GlobalizationManager.Translate(errorMessageKey, validationRule.FieldName + " Min Length: ", FrameworkToken.UserToken.CurrentSite.Id) + validationRule.MinLength.Value;
                    if (value.ToString().Length < validationRule.MinLength.Value)
                    {
                        return false;
                    }

                }
                if (validationRule.MaxLength.HasValue)
                {
                    errorMessageKey += "MaxMinLength";
                    this.ErrorMessage = GlobalizationManager.Translate(errorMessageKey, validationRule.FieldName + " MAX Length: ", FrameworkToken.UserToken.CurrentSite.Id) + validationRule.MaxLength.Value;
                    if (value.ToString().Length > validationRule.MaxLength.Value)
                    {
                        return false;
                    }
                }
                if (validationRule.Min.HasValue)
                {
                    decimal v = 0;
                    if (decimal.TryParse(value.ToString(), out v))
                    {
                        if (v < validationRule.Min.Value)
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (validationRule.Max.HasValue)
                {
                    decimal v = 0;
                    if (decimal.TryParse(value.ToString(), out v))
                    {
                        if (v > validationRule.Max.Value)
                        {
                            return false;
                        }
                        else
                        {
                            return true;
                        }
                    }
                    else
                    {
                        return false;
                    }

                }
                if (validationRule.IsNumber)
                {
                    errorMessageKey += "IsNumber";
                    this.ErrorMessage = GlobalizationManager.Translate(errorMessageKey, validationRule.FieldName + " Is Number: ", FrameworkToken.UserToken.CurrentSite.Id) + validationRule.IsNumber;
                    decimal v = 0;
                    if (decimal.TryParse(value.ToString(), out v) == false)
                    {
                        return false;
                    }
                }
                if (validationRule.IsEmail)
                {
                    errorMessageKey += "EmailFormat";
                    this.ErrorMessage = GlobalizationManager.Translate(errorMessageKey, validationRule.FieldName + " is invalid E-mail ", FrameworkToken.UserToken.CurrentSite.Id);

                    if (!Regex.IsMatch(value.ToString(), @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"))
                    {
                        return false;
                    }
                }
                if (!string.IsNullOrEmpty(validationRule.RegExpression))
                {
                    this.ErrorMessage = validationRule.RegExpression;
                }
            }

            return true;

        }

        public override string FormatErrorMessage(string name)
        {
            return this.ErrorMessage;
        }

    }
}
